/* @(#)Bbs.java    V1.0
 *
 * Project: APEP
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   wangp    {CREATE_TIME} First release
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2007 Auto-Parts E-Procurement Co., Ltd.
 *        No.251-1 Wuning Road, Shanghai, 200063, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Auto-Parts E-Procurement Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with APEP.
 *
 * Warning:
 * =============================================================================
 * 
 * 
 */
package system.base;

import system.data.BbsPageData;
import framework.core.ConstDefine;
import framework.core.DBConnection;
import framework.core.DateUtil;
import framework.core.IHandler;
import framework.core.ILoginContext;
import framework.core.Logger;
import framework.core.ObjectList;
import framework.core.PageData;
import framework.core.SearchEvent;
import framework.core.SrvContext;
import framework.core.Value;
/*
	Author    :wangp
	CreateTime:{CREATE_TIME}
 */
public class BbsSrv extends IHandler
{

	public BbsSrv()
	{
		super();
	}
	private void deleteBbs(DBConnection con,SrvContext context) throws Exception
	{
		int[] ids = context.getInts("ids");
		Bbs obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = Bbs.fetch(con,ids[i]);
			if(obj!=null) {
				obj.delete(con);
			}
		}
	}
	private void saveBbs(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		int id = context.getInt("Id",Value.NULL_INT);
		Bbs obj;
		if(id==Value.NULL_INT)   //add
		{
			obj = Bbs.fetch();
		}
		else                     //update
		{
			obj = Bbs.fetch(con,id);
		}
		BbsPageData data = (BbsPageData)PageData.fromForm(context,BbsPageData.class);
		context.setValue("BbsPageData",data);
		obj.fill(data);

		obj.setCreateTime(new DateUtil().getFullString());
		obj.setCreateUser(loginContext.getLoginUserName());
		obj.setCreateUserId(loginContext.getLoginUserId());
		if(obj.getRelId() == Value.NULL_INT) {
			obj.setRelId(Bbs.FIRST_SUBJECT);
			obj.setReplyNum(0);
			obj.setBrowerNum(0);
		} else {
			Bbs tOrgBbs = Bbs.fetch(con, obj.getRelId());
			if(tOrgBbs != null) {
				tOrgBbs.setReplyNum(tOrgBbs.getReplyNum() + 1);
				tOrgBbs.save(con);
			}
		}
		obj.save(con);
	}
	private void listBbs(DBConnection con,ILoginContext loginContext, SrvContext context,String path) throws Exception
	{
		BbsSearchEvent event = (BbsSearchEvent)SearchEvent.fromForm(context,BbsSearchEvent.class);

		if( ConstDefine.STR_YES.equalsIgnoreCase(event.getRelUserId())) {
			event.setRelUserId(loginContext.getLoginUserId());
		} else {
			event.setRelUserId("");
		}


		event.setRelId(Bbs.FIRST_SUBJECT);
		event.setOrderDesc(true);
		context.setSessionValue("BbsSearchEvent",event);
		context.setNextUrl(path+"BbsList.jsp");
	}
	private void backBbs(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"BbsList.jsp");
	}

	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";

		if(action_code.equalsIgnoreCase("listBbs"))
		{
			listBbs(con,loginContext, context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewBbs"))
		{
			int id = context.getInt("Id");
			ObjectList tBbsList = Bbs.getListByRootId(con, id);
			Bbs obj = Bbs.fetch();
			BbsPageData data = (BbsPageData)PageData.fromTable(obj,BbsPageData.class);
			context.setValue("BbsPageData",data);
			context.setSessionValue("BbsList", tBbsList);
			context.setNextUrl(path+"BbsView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteBbs"))
		{
			context.setErrorUrl(path+"BbsList.jsp");
			deleteBbs(con,context);
			backBbs(con,context,path);
			return;
		}

		if(action_code.equalsIgnoreCase("toAddBbs"))
		{
			Bbs obj = Bbs.fetch();
			BbsPageData data = (BbsPageData)PageData.fromTable(obj,BbsPageData.class);
			context.setValue("BbsPageData",data);
			context.setNextUrl(path+"BbsEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateBbs"))
		{
			int id = context.getInt("Id");
			Bbs obj = Bbs.fetch(con,id);
			BbsPageData data = (BbsPageData)PageData.fromTable(obj,BbsPageData.class);
			context.setValue("BbsPageData",data);
			context.setNextUrl(path+"BbsEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyBbs"))
		{
			int id = context.getInt("Id");
			Bbs obj = Bbs.copy(Bbs.fetch(con,id));
			BbsPageData data = (BbsPageData)PageData.fromTable(obj,BbsPageData.class);
			context.setValue("BbsPageData",data);
			context.setNextUrl(path+"BbsEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveBbs"))
		{
			context.setErrorUrl(path+"BbsEdit.jsp");
			saveBbs(con,context,loginContext);
			backBbs(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewBbs"))
		{
			context.setErrorUrl(path+"BbsEdit.jsp");
			saveBbs(con,context,loginContext);
			Bbs obj = Bbs.fetch();
			BbsPageData data = (BbsPageData)PageData.fromTable(obj,BbsPageData.class);
			context.setValue("BbsPageData",data);
			context.setNextUrl(path+"BbsEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("backListBbs"))
		{
			backBbs(con,context,path);
			return;
		}

		//add by wangp start

		//add by wangp end

		Logger.throwException("BbsSrv","unknown action_code="+action_code);
	}
}
